package com.platform.server.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.paltform.server.entity.ExcelCompany;
import com.paltform.server.entity.ExcelEnterprise;
import com.platform.server.dao.CompanyDao;
import com.platform.server.service.CompanyService;
import com.platform.server.service.UserService;
import com.platform.server.util.ExportExcel;
import com.platform.server.util.StringTools;

@Controller
public class EnterpriseUserController {
	
	
	@Autowired
	private UserService mapService;
	
	
	
	/**
	 * 企业用户列表
	 * @return
	 */
	@RequestMapping("/selectUserCompanyList")
	public Object selectCompanyList(HttpServletRequest request,String mapid,String audit,String mapname,String mapName,String startTime,String endTime,String mobile,String addres,String isUser, String gender,String mapMobile, String addrees){
		List<Map<String, Object>> lists = mapService.selectEnterpriseUserList(mapid, startTime, endTime, mapname, mobile, addres, isUser);
		for (Map<String, Object> map : lists) {
			/*if(map.get("expire_time")!=null){
			    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			    try {
					Date dt1 = df.parse(map.get("expire_time").toString());
					if(dt1.getTime()>=new Date().getTime()){
						map.put("expire_time", "1");
						}else{
							map.put("expire_time", "0");
						}
				} catch (ParseException e) {
					// TODO Auto-generated catch block
				continue;
				}
			}*/
		}
		request.setAttribute("lists", lists);
		request.setAttribute("nums", lists.size());
		if(!StringTools.isEmpty(mapid)){
			List<Map<String,Object>> selectEntUserList = mapService.selectEntUserList(mapid, mapName, mapMobile, addrees, gender);
			request.setAttribute("mapList", selectEntUserList);
			request.setAttribute("numss", selectEntUserList.size());
			return "/ui/company/enterprisecompany_info.jsp";
		}
		return "/ui/company/enterprisecompany_list.jsp";
	}
	/**
	 * 企业服务列表  -
	 * @return
	 */
	@RequestMapping("/selectEntUserOrderList")
	public Object selectEntUserOrderList(HttpServletRequest request,String mapid, String startTime,String endTime,String server_type, String staff_id,String addres, String gender){
		List<Map<String, Object>> lists = mapService.selectEntUserOrderList(mapid, startTime, endTime, server_type, staff_id, addres, gender);
		request.setAttribute("lists", lists);
		request.setAttribute("nums", lists.size());
		return "/ui/company/enterpriseorder_list.jsp";
	}
	
	
	
	// 导出报表
		@RequestMapping("/enterpriseExportExcel")
		public void companyExportExcel(HttpServletResponse response, HttpServletRequest request,String mapid,String audit,String mapname,String startTime,String endTime,String mobile,String addres,String isUser){
			List<Object> list2 = new ArrayList<Object>();
			List<Map<String, Object>> lists = mapService.selectEnterpriseUserList(mapid, startTime, endTime, mapname, mobile, addres, isUser);
			for (Map<String, Object> map : lists) {
				ExcelEnterprise excelEnterprise = new ExcelEnterprise();
				excelEnterprise.setPhoto(map.get("photo"));
				excelEnterprise.setUsername(map.get("mapname"));
				excelEnterprise.setMobile(map.get("mobile"));
				excelEnterprise.setId(map.get("id"));
				if(map.get("gender").toString().equals("2")){
					excelEnterprise.setGender("女");
				}else if (map.get("gender").toString().equals("1")){
					excelEnterprise.setGender("男");
				}else{
					excelEnterprise.setGender("未知");
				}
				excelEnterprise.setIntegral_rank(map.get("integral_rank"));
				if(map.get("audit")!=null&&map.get("audit").toString().equals("1")){
					excelEnterprise.setAudit("已认证");
				}else{
					excelEnterprise.setAudit("未认证");
				}
				if(map.get("enteraudit")!=null&&map.get("enteraudit").toString().equals("1")){
					excelEnterprise.setEnteraudit("已认证");
				}else{
					excelEnterprise.setEnteraudit("未认证");
				}
				excelEnterprise.setLogin_time(map.get("login_time"));
				excelEnterprise.setAddress(map.get("address"));
				
				if(map.get("vip")==null){
					excelEnterprise.setExpire_time( "普通用户");
				}
				if(map.get("vip")!=null&&map.get("vip").equals("1")){
					excelEnterprise.setExpire_time( "VIP1");
				}
				if(map.get("vip")!=null&&map.get("vip").equals("2")){
					excelEnterprise.setExpire_time( "企业VIP");
				}
				if(map.get("vip")!=null&&map.get("vip").equals("3")){
					excelEnterprise.setExpire_time( "VIP2");
				}
				list2.add(excelEnterprise);
			}
			String[] titles = { "头像", "昵称", "手机号", "ID", "性别", "用户等级", "实名认证", "企业认证","会员" ,"所在地","最近登录时间"};
			ExportExcel.exportExcel("企业用户列表.xls", titles, list2, response);
		}
}
